package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JSlider;
import javax.swing.JLabel;

import com.alee.utils.swing.Timer;
import com.it2297.main.CompanyApp;

import javax.swing.JButton;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.UIManager;

public class CompanyHomePage extends JFrame {

	private JMenuBar menuBar;
	private JMenu menuHome;
	private JMenu menuExit;
	private JMenu menuJobAd;
	private JMenuItem menuRemoveJob;
	private JMenuItem menuCreateJob;
	private JMenuItem menuViewAllJobsByCompany;
	private JMenu menuApplicants;
	private JMenuItem menuViewApplicants;
	private JSeparator separator;
	private Component horizontalGlue;
	private Component horizontalStrut;
	private JLabel lblWelcome;
	private JMenuItem menuLogout;
	private JMenuItem menuhomescreen;
	private JMenu mnProfile;
    private JMenuItem mntmUpdateCompany;
	
	
	private JPanel contentPane;
	private JSlider slider;
	private JLabel lbl;
	private Timer timer;
	private JButton btnStart;
	private int i=1;
	private JButton btnStop;
	private JLabel lbl2;
	String currentWorkingDirectory = System.getProperty("user.dir");
	private JLabel lblWelcomeClickOn;
	private static JLabel lblTime;
	
	
	
	public void setTimer(String begin){
		if(begin=="start"){
		
		
		btnStart.setEnabled(false);
  		i = i;
  		timer.start();
  		CurrentDate();
		}
	}
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CompanyHomePage frame = new CompanyHomePage();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public CompanyHomePage() {
		setTitle("Company - Home Screen");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		slider = new JSlider();
		slider.setValue(1);
		slider.setMinimum(1);
		slider.setMaximum(3);
		slider.setBounds(148, 35, 428, 32);
		slider.setBackground(Color.DARK_GRAY);
		slider.setToolTipText("");
		slider.setForeground(Color.WHITE);
		slider.setBorder(new MatteBorder(4, 4, 4, 4, (Color) Color.BLACK));
		slider.setMajorTickSpacing(1);
		slider.setPaintTicks(true);
		contentPane.add(slider);
		event e = new event();
		slider.addChangeListener(e);
		slider.setVisible(false);
		
		lbl = new JLabel();
		lbl.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {
				if(lbl2.getText()=="Post Job Advertisements!"){
					JobCreateJob jcj=new JobCreateJob();
					jcj.setLocationRelativeTo(null);
					jcj.setVisible(true);
					jcj.setTitle("Company - Create Job Advertisement ");
					closeWindow();
				}
				else if(lbl2.getText()=="View All Jobs Advertisements Posted By You!"){
					CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
					cvajbc.setLocationRelativeTo(null);
					cvajbc.setVisible(true);
					cvajbc.setTitle("Company - View All Jobs By Company ");
					closeWindow();
				}
				else if(lbl2.getText()=="Remove Job Advertisements!"){
					JobRemoveJob jrj=new JobRemoveJob();
					jrj.setLocationRelativeTo(null);
					jrj.setVisible(true);
					jrj.setTitle("Company - Remove Job Advertisement ");
					closeWindow();
				}
			}
		});
		lbl.setBackground(new Color(255, 255, 255));
		lbl.setHorizontalAlignment(SwingConstants.CENTER);
		lbl.setIcon(new ImageIcon(currentWorkingDirectory+"/img/Job/Postjob.jpg"));
		lbl.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
		lbl.setBounds(10, 161, 704, 350);
		contentPane.add(lbl);
		
		btnStart = new JButton();
		btnStart.setBackground(UIManager.getColor("Button.background"));
		btnStart.setIcon(new ImageIcon(currentWorkingDirectory+"/img/Job/Play.jpg"));
		btnStart.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnStart.addActionListener(new ButtonListener());
		btnStart.setBounds(326, 78, 27, 24);
		contentPane.add(btnStart);
		
		btnStop = new JButton();
		btnStop.setBackground(UIManager.getColor("Button.background"));
		btnStop.setIcon(new ImageIcon(currentWorkingDirectory+"/img/Job/Stop.jpg"));
		btnStop.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				timer.stop();
				btnStart.setEnabled(true);
			}
		});
		btnStop.setBounds(367, 78, 22, 24);
		contentPane.add(btnStop);
		
		lblWelcomeClickOn = new JLabel("Click on any of the images and you will directed to the respective pages!");
		lblWelcomeClickOn.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcomeClickOn.setForeground(Color.WHITE);
		lblWelcomeClickOn.setFont(new Font("Times New Roman", Font.BOLD, 16));
		lblWelcomeClickOn.setBounds(10, 100, 704, 32);
		contentPane.add(lblWelcomeClickOn);
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(255, 255, 255));
		panel.setBounds(10, 161, 704, 350);
		contentPane.add(panel);
		
		lbl2 = new JLabel("Post Job Advertisements!");
		lbl2.setBackground(Color.WHITE);
		lbl2.setHorizontalAlignment(SwingConstants.CENTER);
		lbl2.setFont(new Font("Tahoma", Font.BOLD, 18));
		lbl2.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
		lbl2.setBounds(10, 127, 704, 36);
		contentPane.add(lbl2);
		
		  timer = new Timer(2000, new ActionListener() {
			  public void actionPerformed(ActionEvent evt) {
		
				  if(i==3){
					  i=0;
					  
				  }
			   i = i + 1; 
			 //  System.out.println(i);
			  slider.setValue(i);  
		  }
	 });
		  
		  
		  
		  
		  menuBar = new JMenuBar();
			menuBar.setBounds(0, 0, 724, 24);
			contentPane.add(menuBar);
			
			menuHome = new JMenu("Home");
			menuBar.add(menuHome);
			menuhomescreen=new JMenuItem("Home Screen");
			menuhomescreen.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CompanyHomePage chp=new CompanyHomePage();
					chp.setLocationRelativeTo(null);
					chp.setVisible(true);
					chp.setTimer("start");
					chp.setTitle("Company - Home Screen");
					closeWindow();
				}
			});
			menuHome.add(menuhomescreen);
			
			menuJobAd=new JMenu("Job Advertisement");
			menuBar.add(menuJobAd);
			menuCreateJob=new JMenuItem("Create Job Advertisement");
			menuCreateJob.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JobCreateJob jcj=new JobCreateJob();
					jcj.setLocationRelativeTo(null);
					jcj.setVisible(true);
					jcj.setTitle("Company - Create Job Advertisement ");
					closeWindow();
				}
			});
			menuJobAd.add(menuCreateJob);
			
			JSeparator separator_1 = new JSeparator();
			menuJobAd.add(separator_1);
			menuRemoveJob=new JMenuItem("Remove Job Advertisement");
			menuRemoveJob.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JobRemoveJob jrj=new JobRemoveJob();
					jrj.setLocationRelativeTo(null);
					jrj.setVisible(true);
					jrj.setTitle("Company - Remove Job Advertisement ");
					closeWindow();
				}
			});
			menuViewAllJobsByCompany=new JMenuItem("View All Jobs By Company");
			menuViewAllJobsByCompany.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				//	CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
					ManageJobApplicants cvajbc = new ManageJobApplicants();
					cvajbc.setLocationRelativeTo(null);
					cvajbc.setVisible(true);
					cvajbc.setTitle("Company - View All Jobs By Company ");
					closeWindow();
				}
			});
			menuJobAd.add(menuViewAllJobsByCompany);
			
			JSeparator separator_2 = new JSeparator();
			menuJobAd.add(separator_2);
			menuJobAd.add(menuRemoveJob);
			
			horizontalGlue = Box.createHorizontalGlue();
			menuBar.add(horizontalGlue);
			
			
			
			lblWelcome = new JLabel();
			lblWelcome.setForeground(new Color(0, 0, 128));
			if (CompanyApp.isUserLogin())
			{
				lblWelcome.setText("Welcome, " + CompanyApp.getCurrentUser().getCompanyName() + ".");
			}
			else
			{
				lblWelcome.setText("Welcome, Guest! Please Login.");
			}
			menuBar.add(lblWelcome);
			
			horizontalStrut = Box.createHorizontalStrut(20);
			menuBar.add(horizontalStrut);
			
			mnProfile = new JMenu("Profile");
			menuBar.add(mnProfile);
			
			mntmUpdateCompany = new JMenuItem("Update Company");
			mntmUpdateCompany.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					CompanyUpdateCompany cuc = new CompanyUpdateCompany();
					cuc.setLocationRelativeTo(null);
					cuc.setVisible(true);
					cuc.setTitle("Company - Update Company Details");
					closeWindow();
				}
			});
			
			JMenuItem mntmViewCompany = new JMenuItem("View Company");
			mntmViewCompany.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					CompanyViewDetails cvd= new CompanyViewDetails();
					cvd.setLocationRelativeTo(null);
					cvd.setVisible(true);
					cvd.setTitle("Company - View Company Details");
					closeWindow();
				}
			});
			mnProfile.add(mntmViewCompany);
			
			JSeparator separator_3 = new JSeparator();
			mnProfile.add(separator_3);
			mnProfile.add(mntmUpdateCompany);
			
			
			menuExit = new JMenu("Exit");
			menuBar.add(menuExit);
			menuLogout=new JMenuItem("Logout");
			menuLogout.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CompanyApp.logoutUser();
					
					CompanyLogin loginUI = new CompanyLogin();
					loginUI.setLocationRelativeTo(null);
					loginUI.setVisible(true);
					closeWindow();
				}
			});
			menuExit.add(menuLogout);
			
			lblTime = new JLabel("New label");
			//lblTime.setBorder(new MatteBorder(2, 2, 2, 2, (Color) new Color(0, 0, 0)));
			lblTime.setHorizontalAlignment(SwingConstants.CENTER);
			lblTime.setForeground(Color.WHITE);
			lblTime.setFont(new Font("Times New Roman", Font.BOLD, 30));
			lblTime.setBounds(10, 35, 704, 36);
			contentPane.add(lblTime);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBackground(Color.WHITE);
			panel_1.setBounds(10, 127, 704, 36);
			contentPane.add(panel_1);
			
			JPanel panel_2 = new JPanel();
			panel_2.setBackground(new Color(139, 0, 0));
			panel_2.setBounds(0, 21, 724, 501);
			contentPane.add(panel_2);
	
	}
	
	
	
	public class event implements ChangeListener{
		public void stateChanged(ChangeEvent e){
			
			if(slider.getValue()==1){
				
				lbl.setIcon(new ImageIcon(currentWorkingDirectory+"/img/Job/Postjob.jpg"));
				//lbl.setText("Blah1");
				lbl2.setText("Post Job Advertisements!");
			}
			
			
			if(slider.getValue()==2){
				
				lbl.setIcon(new ImageIcon(currentWorkingDirectory+"/img/Job/Viewjob.jpg"));
			//	lbl.setText("Blah2");
				lbl2.setText("View All Jobs Advertisements Posted By You!");
			}
			
			if(slider.getValue()==3){
				
				lbl.setIcon(new ImageIcon(currentWorkingDirectory+"/img/Job/Removejob.jpg"));
			//	lbl.setText("Blah3.");
				lbl2.setText("Remove Job Advertisements!");
			}
			
		}
	}
	
	  class ButtonListener implements ActionListener {
		  public void actionPerformed(ActionEvent ae) {
			  		btnStart.setEnabled(false);
			  		i = i;
			  		timer.start();
		  	}
		  }
	  
	  
	  public void closeWindow(){
		  this.dispose();
	  }
	  

		
	public static void CurrentDate(){
			
			
			Thread clock = new Thread(){
			public void run(){
				/*for(;;) {} == while(true) {} == do {} while(true);*/
				//infinite loop
				for(;;){
					Calendar cal = new GregorianCalendar();
					int month = cal.get(Calendar.MONTH);
					int year = cal.get(Calendar.YEAR);
					int day = cal.get(Calendar.DAY_OF_MONTH);
			
					int second = cal.get(Calendar.SECOND);
					int minute = cal.get(Calendar.MINUTE);
					int hour = cal.get(Calendar.HOUR_OF_DAY);
					lblTime.setText("Date: " + day + "/" + (month+1) + "/" + year + ", Time: " + hour + ":"+(minute)+":"+second);
						try {
							sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
			}
		};
		clock.start();
		}
}
	  
  
	  

